package com.haitaoit.chengquan.module.center.activity.yuyue;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.center.model.MyYuyueDetailModel;
import com.haitaoit.chengquan.module.home.activity.maintenance.ServerPayActivity;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.DialogUtil;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyYuyueDetailActivity extends BaseActivity {
    //    @ViewInject(R.id.scrollview_listview)
//    MyListView listView;
    @ViewInject(R.id.yuyue_detail_state)
    TextView yuyue_detail_state;
    @ViewInject(R.id.yuyue_detail_price)
    TextView yuyue_detail_price;
    @ViewInject(R.id.detail_time1)
    TextView detail_time1;
    @ViewInject(R.id.detail_time2)
    TextView detail_time2;
    @ViewInject(R.id.detail_time3)
    TextView detail_time3;
    @ViewInject(R.id.cancel_order)
    TextView cancel_order;
    @ViewInject(R.id.go_eva)
    TextView go_eva;
    @ViewInject(R.id.go_pay)
    TextView go_pay;
    @ViewInject(R.id.submit_finish)
    TextView submit_finish;
    @ViewInject(R.id.detele_yuyue)
    TextView detele_yuyue;
    @ViewInject(R.id.accept_name)
    TextView accept_name;
    @ViewInject(R.id.telphone)
    TextView telphone;
    @ViewInject(R.id.area_address)
    TextView area_address;
    @ViewInject(R.id.order_img)
    ImageView order_img;
    @ViewInject(R.id.order_title)
    TextView order_title;
    @ViewInject(R.id.yuyue_content)
    TextView yuyue_content;
    @ViewInject(R.id.yuyue_money)
    TextView yuyue_money;
    @ViewInject(R.id.user_remark)
    TextView user_remark;
    @ViewInject(R.id.service_time)
    TextView service_time;
    @ViewInject(R.id.detail_buttonLL)
    LinearLayout detail_buttonLL;


    // private MyYuyueDetailAdapter adapter;
    private String order_no, title_type;
    private MyYuyueDetailModel model;
    private Dialog dlg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_my_yuyue_detail);
        setTitle_V("预约详情");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);

//        adapter = new MyYuyueDetailAdapter(this);
//        listView.setAdapter(adapter);

        title_type = getIntent().getStringExtra("title_type");
        order_no = getIntent().getStringExtra("order_no");
    }

    @Override
    protected void onResume() {
        if ("1".equals(title_type)) {
            httpGetuser_service_order_model(Constant.Getuser_service_order_model);
        } else if ("2".equals(title_type)) {
            httpGetuser_service_order_model(Constant.Getuser_service_jcgz_order_model);
        }
        super.onResume();
    }

    @OnClick({R.id.cancel_order, R.id.go_eva, R.id.go_pay, R.id.submit_finish,
            R.id.detele_yuyue,})
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.cancel_order:// 取消订单
                dlg = DialogUtil.showInfoDialog1(this, "提示", "您确定要取消该预约订单吗？", "确定取消", "再想想", new BackCall() {
                    public void deal(int which, Object... obj) {
                        switch (which) {
                            case R.id.confirm:// 确定
                                httpGetcancel_order(order_no);
                                break;
                        }
                        super.deal(which);
                    }
                });
                dlg.setCanceledOnTouchOutside(true);
                dlg.show();
                break;
            case R.id.go_eva:// 去评价
                intent.setClass(this, YuyueEvaluateActivity.class);
                intent.putExtra("order_no", order_no);
                intent.putExtra("type", "1");
                intent.putExtra("myimage_uri", model.getTitile_img());
                startActivity(intent);
                break;
            case R.id.go_pay:// 付款
                intent.setClass(this, ServerPayActivity.class);
                intent.putExtra("order_no", order_no);
                intent.putExtra("order_amount", model.getOrder_amount());
                startActivity(intent);
                break;
            case R.id.submit_finish:// 确认完成
                dlg = DialogUtil.showInfoDialog1(this, "提示", "您确定该预约订单已完成吗？", "确定", "还没有", new BackCall() {
                    public void deal(int which, Object... obj) {
                        switch (which) {
                            case R.id.confirm:// 确定
                                httpGetservice_complete_order(order_no);
                                break;
                        }
                        super.deal(which);
                    }
                });
                dlg.setCanceledOnTouchOutside(true);
                dlg.show();
                break;
            case R.id.detele_yuyue:// 删除订单
                dlg = DialogUtil.showInfoDialog1(this, "提示", "您确定要删除该预约订单吗？", "确定", "取消", new BackCall() {
                    public void deal(int which, Object... obj) {
                        switch (which) {
                            case R.id.confirm:// 确定
                                httpGetdel_order(order_no);
                                break;
                        }
                        super.deal(which);
                    }
                });
                dlg.setCanceledOnTouchOutside(true);
                dlg.show();
                break;
        }
        super.onClick(v);
    }

    private void httpGetuser_service_order_model(String url) {
        final List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String result = arg0.result.toString();
                try {
                    JSONObject json = new JSONObject(result);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    Gson gson = new Gson();
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            break;
                        case 0:// 成功
                            model = gson.fromJson(Response, MyYuyueDetailModel.class);
                            /**
                             * status:订单状态1未支付(显示取消订单和付款)2已支付(is_confirm:1显示完成按钮2不显示)
                             * 3已完成(is_evaluation为1显示区评价)4已评价5已取消(显示删除订单)
                             *
                             * status:订单状态1已下单(显示取消订单)
                             * 2已确认(显示取消订单和去付款 is_update_order:1不显示付款2显示付款)
                             * 3已支付(is_confirm:1显示完成按钮2不显示)4已完成(is_evaluation为1显示区评价)
                             * 5已评价6已取消(显示删除订单)*/
                            cancel_order.setVisibility(View.GONE);
                            go_pay.setVisibility(View.GONE);
                            go_eva.setVisibility(View.GONE);
                            submit_finish.setVisibility(View.GONE);
                            detele_yuyue.setVisibility(View.GONE);
                            detail_buttonLL.setVisibility(View.VISIBLE);
                            detail_time3.setVisibility(View.VISIBLE);
                            if ("1".equals(title_type)) {
                                if (model.getStatus().equals("1")) {
                                    yuyue_detail_state.setText("未支付");
                                    detail_time3.setVisibility(View.GONE);
                                    cancel_order.setVisibility(View.VISIBLE);
                                    go_pay.setVisibility(View.VISIBLE);
                                } else if (model.getStatus().equals("2")) {
                                    yuyue_detail_state.setText("已支付");
                                    if (model.getIs_confirm().equals("1")) {
                                        submit_finish.setVisibility(View.VISIBLE);
                                    } else {
                                        detail_buttonLL.setVisibility(View.GONE);
                                    }
                                } else if (model.getStatus().equals("3")) {
                                    yuyue_detail_state.setText("已完成");
                                    if (model.getIs_evaluation().equals("1")) {
                                        go_eva.setVisibility(View.VISIBLE);// 去评价
                                    } else {
                                        detail_buttonLL.setVisibility(View.GONE);
                                    }
                                } else if (model.getStatus().equals("4")) {
                                    yuyue_detail_state.setText("已评价");
                                    detail_buttonLL.setVisibility(View.GONE);
                                } else if (model.getStatus().equals("5")) {
                                    yuyue_detail_state.setText("已取消");
                                    detele_yuyue.setVisibility(View.VISIBLE);
                                    detail_time3.setVisibility(View.GONE);
                                }
                            } else if ("2".equals(title_type)) {
                                if (model.getStatus().equals("1")) {
                                    yuyue_detail_state.setText("已下单");
                                    detail_time3.setVisibility(View.GONE);
                                    cancel_order.setVisibility(View.VISIBLE);
                                } else if (model.getStatus().equals("2")) {
                                    yuyue_detail_state.setText("已确认");
                                    detail_time3.setVisibility(View.GONE);
                                    cancel_order.setVisibility(View.VISIBLE);// 取消订单
                                    if (model.getIs_update_order().equals("1")) {
                                        go_pay.setVisibility(View.GONE);// 付款
                                    } else {
                                        go_pay.setVisibility(View.VISIBLE);// 付款
                                    }
                                } else if (model.getStatus().equals("3")) {
                                    yuyue_detail_state.setText("已支付");
                                    if (model.getIs_confirm().equals("1")) {
                                        submit_finish.setVisibility(View.VISIBLE);
                                    } else {
                                        detail_buttonLL.setVisibility(View.GONE);
                                    }
                                } else if (model.getStatus().equals("4")) {
                                    yuyue_detail_state.setText("已完成");
                                    if (model.getIs_evaluation().equals("1")) {
                                        go_eva.setVisibility(View.VISIBLE);// 去评价
                                    } else {
                                        detail_buttonLL.setVisibility(View.GONE);
                                    }
                                } else if (model.getStatus().equals("5")) {
                                    yuyue_detail_state.setText("已评价");
                                    detail_buttonLL.setVisibility(View.GONE);
                                } else if (model.getStatus().equals("6")) {
                                    yuyue_detail_state.setText("已取消");
                                    detail_time3.setVisibility(View.GONE);
                                    detele_yuyue.setVisibility(View.VISIBLE);
                                }
                            }
                            accept_name.setText("联系人：" + model.getAccept_name());
                            telphone.setText(model.getTelphone());
                            area_address.setText("服务地址：" + model.getArea() + model.getAddress());
                            ImageLoader.getInstance().displayImage(model.getTitile_img(), order_img);
                            order_title.setText(model.getTitle());
                            yuyue_content.setText("分类：" + model.getType());
                            yuyue_money.setText("¥ " + model.getMoney());
                            yuyue_detail_price.setText("¥ " + model.getOrder_amount());
                            if (model.getUser_remark().equals("")) {
                                user_remark.setText("无");
                            } else {
                                user_remark.setText(model.getUser_remark());
                            }
                            service_time.setText(model.getService_time());
                            detail_time1.setText("预约编号：" + model.getOrder_no());
                            detail_time2.setText("创建时间：" + model.getAddtime());
                            detail_time3.setText("付款时间：" + model.getPay_time());
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 需求服务完成订单
    private void httpGetservice_complete_order(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getservice_complete_order, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            if ("1".equals(title_type)) {
                                httpGetuser_service_order_model(Constant.Getuser_service_order_model);
                            } else if ("2".equals(title_type)) {
                                httpGetuser_service_order_model(Constant.Getuser_service_jcgz_order_model);
                            }
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 取消订单
    private void httpGetcancel_order(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getcancel_order, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 删除订单
    private void httpGetdel_order(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getdel_order, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
